//
//  LSWAdViewController.m
//  BaiSi
//
//  Created by 李善武 on 16/5/13.
//  Copyright © 2016年 李善武. All rights reserved.
//

#import "LSWAdViewController.h"
#import "LSWTabBarController.h"
#import <AFNetworking.h>
#import <MJRefresh.h>
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
#import "LSWAdItem.h"

#define XMGCode2 @"phcqnauGuHYkFMRquANhmgN_IauBThfqmgKsUARhIWdGULPxnz3vndtkQW08nau_I1Y1P1Rhmhwz5Hb8nBuL5HDknWRhTA_qmvqVQhGGUhI_py4MQhF1TvChmgKY5H6hmyPW5RFRHzuET1dGULnhuAN85HchUy7s5HDhIywGujY3P1n3mWb1PvDLnvF-Pyf4mHR4nyRvmWPBmhwBPjcLPyfsPHT3uWm4FMPLpHYkFh7sTA-b5yRzPj6sPvRdFhPdTWYsFMKzuykEmyfqnauGuAu95Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiu9mLfqHbD_H70hTv6qnHn1PauVmynqnjclnj0lnj0lnj0lnj0lnj0hThYqniuVujYkFhkC5HRvnB3dFh7spyfqnW0srj64nBu9TjYsFMub5HDhTZFEujdzTLK_mgPCFMP85Rnsnbfknbm1QHnkwW6VPjujnBdKfWD1QHnsnbRsnHwKfYwAwiuBnHfdnjD4rjnvPWYkFh7sTZu-TWY1QW68nBuWUHYdnHchIAYqPHDzFhqsmyPGIZbqniuYThuYTjd1uAVxnz3vnzu9IjYzFh6qP1RsFMws5y-fpAq8uHT_nBuYmycqnau1IjYkPjRsnHb3n1mvnHDkQWD4niuVmybqniu1uy3qwD-HQDFKHakHHNn_HR7fQ7uDQ7PcHzkHiR3_RYqNQD7jfzkPiRn_wdKHQDP5HikPfRb_fNc_NbwPQDdRHzkDiNchTvwW5HnvPj0zQWndnHRvnBsdPWb4ri3kPW0kPHmhmLnqPH6LP1ndm1-WPyDvnHKBrAw9nju9PHIhmH9WmH6zrjRhTv7_5iu85HDhTvd15HDhTLTqP1RsFh4ETjYYPW0sPzuVuyYqn1mYnjc8nWbvrjTdQjRvrHb4QWDvnjDdPBuk5yRzPj6sPvRdgvPsTBu_my4bTvP9TARqnam"
#define baseURL @"http://mobads.baidu.com/cpro/ui/mads.php"
@interface LSWAdViewController ()

// 展示广告控件
@property (weak, nonatomic) UIImageView *imageView;

@property (weak, nonatomic) IBOutlet UIView *adView;

@property (weak, nonatomic) IBOutlet UIImageView *launchImageView;

@property (weak, nonatomic) IBOutlet UIButton *jumpBtn;

@property (nonatomic ,weak) NSTimer *timer;
// 存放模型的数组
@property (nonatomic ,strong) LSWAdItem *adItem;
@end

@implementation LSWAdViewController
// 懒加载定时器
- (NSTimer *)timer{
    if (_timer == nil) {
        NSTimer *timer = [[NSTimer alloc]init];
        _timer = timer;
    }
    return _timer;
}

// 懒加载imageView
- (UIImageView *)imageView{
    if (_imageView == nil) {
        UIImageView *imageView = [[UIImageView alloc]init];
        [self.adView addSubview:imageView];
        _imageView = imageView;
        imageView.userInteractionEnabled = YES;
        
        // 点按手势
        UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
        [imageView addGestureRecognizer:gesture];
    }
    return _imageView;
}

// 执行点按方法
- (void)tap{
   
    // 跳转到广告界面
    UIApplication *app = [UIApplication sharedApplication];
    if ([app canOpenURL:[NSURL URLWithString:_adItem.ori_curl]]) {
        [app openURL:[NSURL URLWithString:_adItem.ori_curl]];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置广告启动界面
    [self setLaunchImage];
    
    // 加载广告数据
    [self loadData];
    
    // 创建定时器
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeChange) userInfo:nil repeats:YES];
    self.timer = timer;
    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
}

// 点击按钮，跳转到根控制器
- (IBAction)btnJump {
    [UIApplication sharedApplication].keyWindow.rootViewController = [[LSWTabBarController alloc]init];
    
    [_timer invalidate];
}

// 执行定时器方法(每隔一秒调用一次)
- (void)timeChange{
    // static全局变量，只在编译的时候执行
    static int i = 3;
    if (i == 0) {
        [self btnJump];
    }
    i--;
    
    // 设置标题按钮
    NSString *str = [NSString stringWithFormat:@"跳过(%d)秒",i];
    [self.jumpBtn setTitle:str forState:UIControlStateNormal];
}

// 加载广告数据
- (void)loadData
{
    
    // 1.创建请求会话管理者
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    // 2.创建请求参数:字典
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"code2"] = XMGCode2;
    
    // 3.发送请求(get,post)
    [mgr GET:@"http://mobads.baidu.com/cpro/ui/mads.php" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary  *_Nullable responseObject) {
        
        // 获取广告字典
        NSDictionary *dict = [responseObject[@"ad"] firstObject];
        // 字典转模型
        self.adItem = [LSWAdItem mj_objectWithKeyValues:dict];
        
        // 防止除数为零
        if (self.adItem.w == 0) {
            return ;
        }
        CGFloat imageW = LSWScreenW;
        CGFloat imageH = self.adItem.h / self.adItem.w * LSWScreenW;
        self.imageView.frame = CGRectMake(0, 0, imageW, imageH);
        
        // 加载广告
        [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.adItem.w_picurl] placeholderImage:nil];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求数据失败");
    }];
}

// 设置广告启动界面
- (void)setLaunchImage{
    
    UIImage *image = nil;
    
    if (iphone6P) { // 6P
        image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h@3x"];
        NSLog(@"%@",image);
    } else if (iphone6) {
        image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    } else if (iphone5) {
        image = [UIImage imageNamed:@"LaunchImage-700-568h"];
    } else if (iphone4) {
        image = [UIImage imageNamed:@"LaunchImage-700"];
    }
    
    self.launchImageView.image = image;
    NSLog(@"%@",self.launchImageView.image);
}
@end
